require(["lang/zhcn"])
require(["tools"]);

function network(){};
network.prototype.socket = null;
network.prototype.socketcallback = null;
network.prototype.ajaxPost = function(url, params, callbackfunc, resent)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            ret = stringtojson(xmlhttp.responseText);
            callbackfunc(ret);
        }
        else if (xmlhttp.readyState == 4 && xmlhttp.status != 200)
        {
            if(resent)
            {
                xmlhttp.open("Post",url,true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//important
                xmlHttp.send(param) ;
            }
            else
            {
                alert(NT_AJAX_errorcontent);
                callbackfunc(null);
            }
        }
    }
    xmlhttp.open("Post",url,true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//important
    xmlHttp.send(param) ;
};

network.prototype.ajaxGet = function(url, callbackfunc, resent)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            ret = stringtojson(xmlhttp.responseText);
            callbackfunc(ret);
        }
        else if (xmlhttp.readyState==4 && xmlhttp.status!=200)
        {
            if( resent )
            {
                xmlhttp.open("GET",url,true);
                xmlhttp.send();
            }
            else
            {
                alert(NT_AJAX_errorcontent);
                callbackfunc(null);
            }
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
};

network.prototype.webConnect = function(url, callbackfunc)
{
    if( this.socket != null )
    {
        alert(NT_WEBSOCKET_reconnecterror)
    }
    this.socket = new WebSocket(url);
    this.socket.onopen = callbackfunc
    this.socket.onclose  = function()
    {
        alert(NT_WEBSOCKET_error)
    }
    this.socket.onmessage = function(evt)
    {
        if( this.socketcallback != null )
        {
            this.socketcallback(evt)
        }
    }
};

network.prototype.webSend = function(item, callbackfunc)
{
    if( this.socket == null )
    {
        alert(NT_WEBSOCKET_notconnected);
    }
    string = jsontostring(item);
    this.socketcallback = callbackfunc
    this.send(string);
};

var network = null